package dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import pojo.Offices;

import java.util.List;

/**
 * @author 李主席
 * @create 2020-12-09 8:58
 */
@Repository
public class OfficesDaoImpl implements OfficesDao {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Offices> selectAllUsers(Integer start, Integer length) {
        //将数据库的一行映射到对象的属性上
        RowMapper<Offices> rowMapper = new BeanPropertyRowMapper<>(Offices.class);
        List<Offices> query = jdbcTemplate.query("select * from offices limit ?, ?", rowMapper, start, length);
        return query;
    }

    @Override
    public Integer selectofficesCount() {

        return jdbcTemplate.queryForObject("select count(*) from offices", Integer.class);
    }

    @Override
    public Integer insertOneOffice(Offices offices) {
        String sql = "insert into offices(name) value(?)";
        return jdbcTemplate.update(sql, offices.getName());
    }
}
